<template>
  <div id="user">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>溯源管理</el-breadcrumb-item>
      <el-breadcrumb-item>病害列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!--搜索框-->
      <el-form :inline="true" :model="searchForm">
        <el-form-item>
          <el-input
              v-model="searchForm.symptomName"
              placeholder="病害名称"
              clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getPlantList">搜索</el-button>
          <el-button type="primary" @click="dialogFormVisible = true" v-if="hasAuth('trace:symptom:save')">新增</el-button>
          <el-popconfirm title="确定要删除这些记录吗？" @confirm="delHandle(null)" style="margin-left: 10px;" v-if="hasAuth('trace:symptom:delete')">
            <el-button type="danger" slot="reference" :disabled="delBtnStu">批量删除</el-button>
          </el-popconfirm>
        </el-form-item>
      </el-form>
      <!--列表-->
      <el-table
          ref="multipleTable"
          border
          stripe
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="plantId"
            label="树木编号">
        </el-table-column>
        <el-table-column
            prop="plantPlotName"
            label="所在林田">
        </el-table-column>
        <el-table-column
            prop="plantTypeName"
            label="所属种类">
        </el-table-column>
        <el-table-column
            label="病害情况">
          <template slot-scope="scope">
            <el-tag style="margin-right: 5px;" size="medium" type="info" v-for="item in scope.row.traceRecords" :key="item.id">{{item.traceSymptom.symptomName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="病害图片">
          <template slot-scope="scope">
            <el-popover v-for="item in scope.row.traceRecords" :key="item.id" placement="top-start" title="" trigger="hover">
              <img :src="item.symptomPic" alt="" style="width: 150px;height: 150px">
              <img slot="reference" :src="item.symptomPic" style="width: 30px;height: 30px">
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间">
        </el-table-column>
        <el-table-column
            width="260px"
            label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="editHandle(scope.row.plantId)" v-if="hasAuth('trace:plant:update')">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="checkHandle(scope.row.plantId)" v-if="hasAuth('trace:plant:list')">查看病害</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-popconfirm title="确定要删除这条记录吗？" @confirm="delHandle(scope.row.plantId)" v-if="hasAuth('trace:plant:delete')">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--页码-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
      <el-dialog title="树木信息" :visible.sync="dialogFormVisible" width="600px">
        <el-form :model="editForm" :rules="editFormRules" ref="editForm">
          <el-form-item label="地块信息" label-width="100px">
            <el-select v-model="editForm.plantPlotId" placeholder="请选择">
              <el-option
                  v-for="item in plotList"
                  :key="item.plotId"
                  :label="item.plotName"
                  :value="item.plotId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="树种信息" label-width="100px">
            <el-select v-model="editForm.plantTypeId" placeholder="请选择">
              <el-option
                  v-for="item in forestList"
                  :key="item.forestId"
                  :label="item.forestName"
                  :value="item.forestId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm('editForm')">取 消</el-button>
          <el-button type="primary" @click="submitEditForm('editForm')">确 定</el-button>
        </div>
      </el-dialog>

<!--      查看病害弹出框-->
      <el-dialog title="病害信息" :visible.sync="symptomFormVisible">
        <el-table
            ref="multipleTable2"
            border
            stripe
            :data="symptomList"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">
          <el-table-column
              type="selection"
              width="55">
          </el-table-column>
          <el-table-column
              label="病害名称">
            <template slot-scope="scope">
              <el-tag style="margin-right: 5px;" size="medium" type="info">{{scope.row.traceSymptom.symptomName}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
              label="病害图片">
            <template slot-scope="scope">
              <el-popover placement="top-start" title="" trigger="hover">
                <img :src="scope.row.symptomPic" alt="" style="width: 150px;height: 150px">
                <img slot="reference" :src="scope.row.symptomPic" style="width: 30px;height: 30px">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
              prop="createTime"
              label="创建时间">
          </el-table-column>
          <el-table-column
              width="260px"
              label="操作">
            <template slot-scope="scope">
              <div v-if="scope.row.status === 1">
                <el-button type="text" disabled @click="editHandle(scope.row.id)" v-if="hasAuth('trace:plant:update')">编辑</el-button>
                <el-divider direction="vertical"></el-divider>
                <el-popconfirm title="确定要删除这条记录吗？" @confirm="delHandle(scope.row.id)" v-if="hasAuth('trace:plant:delete')">
                  <el-button type="text" slot="reference">删除</el-button>
                </el-popconfirm>
              </div>
              <div v-if="scope.row.status === 0">
                <el-button type="text" @click="editSymptomHandle(scope.row.plantId, scope.row.id, 1)" style="color: green" v-if="hasAuth('trace:plant:update')">通过</el-button>
                <el-divider direction="vertical"></el-divider>
                <el-popconfirm title="确定要拒绝这条记录吗？" @confirm="editSymptomHandle(scope.row.plantId, scope.row.id, 2)" v-if="hasAuth('trace:plant:delete')">
                  <el-button type="text" style="color: red" slot="reference">拒绝</el-button>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  name: "Forest",
  data() {
    return {
      searchForm: {
        symptomName: ''
      },
      editForm: {
      },
      editFormRules: {
        symptomName: [
          {required: true, message: '请输入树种名称', trigger: 'blur'}
        ]
      },
      current: 1,
      total: 0,
      size: 10,
      dialogFormVisible: false,
      symptomFormVisible: false,
      tableData: [],
      multipleSelection: [],
      delBtnStu: true,
      roleDialogFormVisible: false,
      roleForm: {
      },
      defaultProps: {
        children: 'children',
        label: 'chineseName'
      },
      roleTreeData:  [],
      treeCheckedKeys: [],
      forestList: [],
      plotList: [],
      symptomList: []
    }
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(rows) {
      this.multipleSelection = rows;
      this.delBtnStu = rows.length === 0
    },
    getPlantList() {
      this.$http.get('/trace/plant/list', {
        params: {
          current: this.current,
          size: this.size
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.data.records
        console.log(this.tableData)
        this.current = res.data.data.current
        this.size = res.data.data.size
        this.total = res.data.data.total
      })
    },
    handleSizeChange(val) {
      this.size = val
      this.getPlantList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getPlantList()
    },
    submitEditForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post('/trace/plant/' + (this.editForm.plantId? "update" : "save") ,this.editForm)
              .then(res => {
                console.log(res.data)
                this.resetForm(formName)
                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                  onClose: () => {
                    this.getPlantList()
                  }
                });
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editHandle(id) {
      console.log(id)
      this.$http.get("/trace/plant/info/" + id).then(res => {
        this.editForm = res.data.data
        this.dialogFormVisible = true
      })
    },
    editSymptomHandle(plantId, id, status) {
      this.$http.get(`/trace/plant/status/${id}/${status}`).then(res => {
        this.checkHandle(plantId)
        this.$message.success("审核成功")
        this.getPlantList()
      })
    },
    checkHandle(id) {
      this.$http.get('/trace/plant/infoSymptom/' + id).then(res => {
        this.symptomList = res.data.data
        console.log("this.symptomList")
        console.log(this.symptomList)
        this.symptomFormVisible = true
      })
    },
    delHandle(id) {
      this.$http.get("/trace/records/delete/"+id).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose: () => {
            this.getPlantList()
          }
        });
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.editForm = {}
      this.dialogFormVisible = false
      this.roleDialogFormVisible = false
    },
    getForestList() {
      this.$http.get('/trace/forest/list', {
        current: 1,
        size: -1
      }).then(res => {
        this.forestList = res.data.data.records
        console.log("forestList")
        console.log(this.forestList)
      })
    },

    getPlotList() {
      this.$http.get('/trace/plot/list', {
        current: 1,
        size: -1
      }).then(res => {
        this.plotList = res.data.data.records
        console.log("plotList")
        console.log(this.plotList)
      })
    }
  },

  created() {
    this.getPlantList()
    this.getForestList()
    this.getPlotList()
  }
}
</script>
<style scoped>
#user {
  margin: 15px 10px;
}
</style>
